package com.utils
{
	import flashx.textLayout.elements.TextFlow;
	
	public class Functions
	{
		import flash.events.*;
		import flash.text.StyleSheet;
		import mx.core.mx_internal;
		import mx.controls.Alert;
		use namespace mx_internal;
		
		public static function getImgSource(item:Object):String {
			if (item.enclosure.@url) {
				return item.enclosure.@url;
			}
			else {
				return "";
			}
		}
		
		public static function parseDate(str:String, s:Boolean = false, nutter:Boolean = false):String {
			var date:Date;
			var weekDayLabels:Array = new Array("Sunday",
				"Monday",
				"Tuesday",
				"Wednesday",
				"Thursday",
				"Friday",
				"Saturday");
			var monthLabels:Array = new Array("January",
				"February",
				"March",
				"April",
				"May",
				"June",
				"July",
				"August",
				"September",
				"October",
				"November",
				"December");
			
			if (s) {
				var comr:RegExp = /EST/;
				str = str.replace(comr,"GMT-5");
				comr = /EDT/;
				str = str.replace(comr,"GMT-4");
			}
			
			if (nutter) {
				// ISO 8601 date time as YYYY-MM-DD hh:mm:ss
				var dateTimeArray:Array = str.split(' ')
				var dateString:String = dateTimeArray[0]
				var timeString:String = dateTimeArray[1]
				
				// parse the date from YYYY-MM-DD
				var pattern:RegExp = /(^\d{4})-(\d{2})-(\d{2}$)/
				var result:Object = pattern.exec( dateString )
				if (result == null)
					throw(new Error("invalid date format"))
				
				var year:uint = new uint( result[1] )
				var month:uint = new uint( result[2] )
				var day:uint = new uint( result[3] )
				
				date = new Date(year, month - 1, day, 0, 0, 0, 0)
				
				// now parse the time from HH:MM:SS
				pattern = /(^\d{2}):(\d{2}):(\d{2})/
				result = pattern.exec( timeString )
				if (result == null)
					throw(new Error("invalid time format"))
				
				var hour:uint = new uint( result[1] )
				var minute:uint = new uint( result[2] )
				var second:uint = new uint( result[3] )
								
				date.setHours(hour, minute, second)
					
				var hr:Number = date.getHours();
				var min:Number = date.getMinutes();
				var m:String = "AM";
				var minStr:String = min.toString();
				if (minStr.length == 1)
					minStr = minStr+"0";
				
				
				if (hr > 12){
					hr = hr-12;
					m = "PM";
				}
				if (hr == 0) {
					hr = 12;			
				} 
				
				return monthLabels[date.getMonth()]+' '+date.getDate()+', ' +date.getFullYear()+' '+hr+':'+minStr+' '+m;
			}
			else {
			date = new Date(str);
			return weekDayLabels[date.getDay()]+', '+monthLabels[date.getMonth()]+' '+date.getDate()+', ' +date.getFullYear();
			}
		}
		
		public static function parseTwitter(str:String):String {
			var ex0:RegExp = /(^(\w+)): /;
			var ex1:RegExp = /(^|[\n ])([\w]+?:\/\/[\w]+[^ \"\n\r\t< ]*)/;
			var ex2:RegExp = /(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)/;
			var ex3:RegExp = /@(\w+)/;
			var ex4:RegExp = /#(\w+)/;
			str = str.replace(ex0, "");
			str = str.replace(ex1, "$1<a href=\"$2\" target=\"_blank\">$2</a>");
			str = str.replace(ex2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
			str = str.replace(ex3, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>");
			str = str.replace(ex4, "<a href=\"http://search.twitter.com/search?q=$1\" target=\"_blank\">#$1</a>");
			return str;
		}
		
		public static function stringToHTML(str:String, nColor:String, hColor:String, wsc:String = "collapse"):TextFlow {
			import flashx.textLayout.conversion.TextConverter;
			import flashx.textLayout.formats.*;
			import flashx.textLayout.elements.Configuration;
			import flashx.textLayout.conversion.ITextImporter;
			
			// Create an importer for TEXT_FIELD_HTML_FORMAT that preserves whitespace.
			// Note: We have to make a copy of the textFlowInitialFormat,
			// which has various formats set to "inherit",
			// and then modify it and set it back.
			var format:TextLayoutFormat;
			var config:Configuration;
			var preservingHTMLImporter:ITextImporter;
			
			config = new Configuration();
			format = new TextLayoutFormat(config.textFlowInitialFormat);
			format.whiteSpaceCollapse = "preserve";
			config.textFlowInitialFormat = format;
			preservingHTMLImporter =
				TextConverter.getImporter(TextConverter.TEXT_FIELD_HTML_FORMAT, config);
			preservingHTMLImporter.throwOnError = false;
			
			
			var tFlow:TextFlow = new TextFlow();
			if (wsc == "preserve")
				tFlow = preservingHTMLImporter.importToFlow(str);
			else
				tFlow = TextConverter.importToFlow(str, TextConverter.TEXT_FIELD_HTML_FORMAT);
			tFlow.linkHoverFormat = { color: hColor, textDecoration: "underline" };
			tFlow.linkNormalFormat = { color: nColor, textDecoration: "none" };
			tFlow.linkActiveFormat = { color: hColor, textDecoration: "underline" };
			return tFlow;
		}
		
		public static function stripHtmlTags(html:String, tags:String = ""):String
		{
			var tagsToBeKept:Array = new Array();
			if (tags.length > 0)
				tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));
			
			var tagsToKeep:Array = new Array();
			for (var i:int = 0; i < tagsToBeKept.length; i++)
			{
				if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
					tagsToKeep.push(tagsToBeKept[i]);
			}
			
			var toBeRemoved:Array = new Array();
			var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
			
			var foundedStrings:Array = html.match(tagRegExp);
			for (i = 0; i < foundedStrings.length; i++) 
			{
				var tagFlag:Boolean = false;
				if (tagsToKeep != null) 
				{
					for (var j:int = 0; j < tagsToKeep.length; j++)
					{
						var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + "( [^<>]*)*>", "i");
						var tmpStr:String = foundedStrings[i] as String;
						if (tmpStr.search(tmpRegExp) != -1) 
							tagFlag = true;
					}
				}
				if (!tagFlag)
					toBeRemoved.push(foundedStrings[i]);
			}
			for (i = 0; i < toBeRemoved.length; i++) 
			{
				var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
				var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
				html = html.replace(tmpRemRE, "");
			} 
			return html;
		}
		
		public static function fixTags(e:String, strip:Boolean, curved:Boolean):String{
			var html:String = e;
			var fr:RegExp = new RegExp("<font([^>]+)>", "g");
			var sr:RegExp = new RegExp("<span([^>]+)>", "g");
			if (curved){
				html = html.replace(new RegExp("&rsquo;", "g"),"’");
				html = html.replace(new RegExp("&rdquo;", "g"),"”");
				html = html.replace(new RegExp("&lsquo;", "g"),"‘");
				html = html.replace(new RegExp("&ldquo;", "g"),"“");
				html = html.replace(new RegExp("&ndash;", "g"),"–");
				
				html = html.replace(new RegExp("\u0091", "g"),"‘");
				html = html.replace(new RegExp("\u0092", "g"),"’");
				html = html.replace(new RegExp("\u0093", "g"),"“");
				html = html.replace(new RegExp("\u0094", "g"),"”");
				html = html.replace(new RegExp("\u0096", "g"),"–");
			}
			else {
				html = html.replace(new RegExp("&rsquo;", "g"),"'");
				html = html.replace(new RegExp("&rdquo;", "g"),"\"");
				html = html.replace(new RegExp("&lsquo;", "g"),"'");
				html = html.replace(new RegExp("&ldquo;", "g"),"\"");
				html = html.replace(new RegExp("&ndash;", "g"),"-");
				
				html = html.replace(new RegExp("’", "g"),"'");
				html = html.replace(new RegExp("”", "g"),"\"");
				html = html.replace(new RegExp("‘", "g"),"'");
				html = html.replace(new RegExp("“", "g"),"\"");
				html = html.replace(new RegExp("–", "g"),"-");
				
				html = html.replace(new RegExp("\u0091", "g"),"'");
				html = html.replace(new RegExp("\u0092", "g"),"'");
				html = html.replace(new RegExp("\u0093", "g"),"\"");
				html = html.replace(new RegExp("\u0094", "g"),"\"");
				html = html.replace(new RegExp("\u0096", "g"),"-");
			}
			html = html.replace(new RegExp("&copy;", "g"),"©");
			html = html.replace(new RegExp("&deg;", "g"),"°");
			
			html = html.replace(fr,"");
			//html = html.replace(sr,"");
			//if (strip)
			//html = stripHtmlTags(html, 'p,br,img,a,i,b');
			return html;
		}
		
		public static function showField(str:String):Boolean {
			if (str == "")
				return false;
			else
				return true;
		}
		
		public static function showStar(str:String):Boolean {
			if (str == "0")
				return false;
			else
				return true;
		}
	}
}